#!/usr/bin/env python
# memory.py
'''
last updated: 04 April 2014

@author: Carl
'''

# import utils

# memory object
class Memory:

    def __init__(self):
        self.savefile = "risave.txt"

    # save highscores, experience, theme (and options)
    # @params {array} highscores (max 10 sorted items)
    # @params {integer} experience
    # @params {string} theme
    # @return {*}
    # example use: memory.risave(highscores, experience, theme)
    def riSave(self, scores, xp, theme):

        # vars
        highscores = scores
        experience = xp
        theme = theme
        # options = {}

        # savefile = "risave.txt"

        i = 1
        hsindex = 0

        try:
            f = open(self.savefile, "w")

            f.write("#\n")

            for score in highscores:
                f.write(score + "\n")

            f.write("\n")
            f.write(str(experience) + "\n")
            f.write("\n")
            f.write(theme)
            f.close()
        except IOError as err:
            print "ErrCode: " + str(err[0]) + ", Message: " + err[1]
        # finally:
            # f.close()

    # load scores, experience, theme (and options)
    # @params {empty}
    # @return {array} highscores
    # @return {integer} experience
    # @return {string} theme
    # example use: scores, xp, theme = memory.riload()
    def riLoad(self):

        # vars
        highscores = []
        experience = 0
        theme = ""
        # options = {}

        # savefile = "risave.txt"

        try:
            f = open(self.savefile, "r")

            i = 1

            lines = f.readlines()

            for line in lines:

                # print line

                # line = f.readline()

                # arr = line.split(" ")

                # empty
                # if i < 2:

                    # jump to next line
                    # print "continue"
                    # continue

                # Scores
                if i > 1 and i < 12:

                    highscores.append(line)
                    # print "scores"
                # Experience
                elif i == 13:
                    experience = line
                    # print "experience"
                # Theme
                elif i == 15:
                    theme = line
                    # print "theme"
                i += 1
                # print i
                # Options
                # TODO: implement options later, when time
                f.close()
        except IOError as err:
            print "ErrCode: " + str(err[0]) + ", Message: " + err[1]
        # finally:
            # f.close()

        return highscores, experience, theme

    def resetSave(self):

        # vars
        highscores = ["500000", "250000", "100000", "50000", "25000", "10000", "5000", "2500", "1000", "500"]
        experience = 0
        theme = "standard"
        # options = {}

        # savefile = "risave.txt"

        # hsindex = 0

        try:
            f = open(self.savefile, "w")

            f.write("#\n")

            i = 0
            for item in highscores:
                f.write(highscores[i] + "\n")
                i += 1

            f.write("\n")
            f.write(str(experience) + "\n")
            f.write("\n")
            f.write(theme + "\n")
            f.close()
        except IOError as err:
            print "ErrCode: " + str(err[0]) + ", Message: " + err[1]
        # finally:
            # f.close()

########
# test #
########
# mem = Memory()
# hs = ["500000", "250000", "100000", "50000", "25000", "10000", "5000", "2500", "1000", "700"]
# xp = 50
# theme = "dark"
# mem.riSave(hs, xp, theme)
# resetSave()
# scores, exp, th = riLoad()
# print scores
# print exp
# print th
# mem.resetSave()
# mem.riLoad()
